<?php 
/*
   Copyright 2010, Sanket Parab

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
   
   This is part of the e-TAT Architecture Package and will be featured in the final e-TAT Application (Phase 1).
   
   Question class: Holds data on each question that is part of a survey
 */
class Question{
	/**
	 * Constructor
	 * @param $id ID of the Question
	 */
	function __construct($id) {
		$query = mysql_query("SELECT * FROM `question` WHERE `id` = '$id'") or die(mysql_error());
		$result = mysql_fetch_object($query);
		if($result) {
			foreach($result as $key=>$value) {
				$this->$key = $value;
			}
		}
	}
	/**
	 * Updates the question
	 * @param $data data string required
	 */
	public function updateQuestion($data) {
		$sql = mysql_query("UPDATE `question` SET
			`type` = '$data->type',
			`required` = '$data->required',
			`question` = '$data->question',
			`create_section` = '$data->create_section',
			`order`='$data->order',
			`response1`='$data->response1',
			`response2`='$data->response2',
			`response3`='$data->response3',
			`response4`='$data->response4',
			`response5`='$data->response5',
			`response6`='$data->response6'
			WHERE `id`='$this->id';")
		or die(mysql_error());
	}
	/**
	 * Checks condition required to be fulfilled in order to display this question
	 * @param $id requires the condition id
	 */
	public function checkCondition($id) {
		$sql = "SELECT * FROM `branching` WHERE `question_1_id` = '$id'";
		$result = mysql_query($sql)or die("Check Condition:".mysql_error());
		$i = 0;
		while($c = mysql_fetch_object($result)) {
			$condition[$i]['question_2_id'] = $c->question_2_id;
			$condition[$i]['condition']=$c->condition;
			$condition[$i]['condition_value']= $c->value;
			$i++;
		}
		if($condition) {
			return $condition;
		} else {
			return false;
		}
	}
	/**
	 * Deletes one of the condiitons created for this question
	 * @param $id requires the condition id
	 */
	public function deleteCondition($id) {
		$sql = "DELETE FROM `branching` WHERE `id`='$id'";
		$result = mysql_query($sql) or die("Delete Condition:".mysql_error());
	}
}
?>